<route>
{
  "meta":{
    "title":"数据字典",
    "showMenu":true,
    "serialNum": 5
  }
}
</route>

<template>
  <div>
    <el-row :gutter="12">
      <el-col :span="6">
        <el-input
          v-model="queryData.type"
          placeholder="字典类型"
          clearable
          :maxlength="64"
          prefix-icon="el-icon-search"
        />
      </el-col>
      <el-col :span="6">
        <el-button type="primary" @click="loadData(1)">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-col>
      <el-col class="mt10">
        <el-button type="primary" @click="handleCreate">新建</el-button>
      </el-col>
    </el-row>
    <el-table
      class="mt10"
      :data="tableData"
      v-loading="loading"
      style="width: 100%"
      current-row-key="id"
      stripe
      border
    >
      <el-table-column prop="optionName" label="文本"/>
      <el-table-column prop="optionValue" label="值"/>
      <el-table-column prop="type" label="字典类型"/>
      <el-table-column prop="serialNum" label="排序"/>
      <el-table-column prop="modifyDate" label="更新时间">
        <template slot-scope="scope">
          {{ scope.row.modifyDate | moment }}
        </template>
      </el-table-column>

      <el-table-column prop="actions" label="操作">
        <template slot-scope="scope">
          <template>
            <span class="table-actionItem"><el-button type="text" @click="handleEdit(scope.row)">编辑</el-button></span>
            <span class="table-actionItem">
              <el-popconfirm
                title="确定要删除该字典吗？"
                placement="top"
                @confirm="handleDelete(scope.row)"
              >
                <el-button class="link-danger" type="text" slot="reference">删除</el-button>
              </el-popconfirm>
            </span>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <div class="tr mt24">
      <x-pagination
        :total="total"
        :pageSize.sync="pageData.pageSize"
        :pageNo.sync="pageData.pageNo"
        @search="loadData"
      />
    </div>

    <DictForm ref="DictForm" @ok="loadData(pageData.pageNo)"/>

  </div>
</template>

<script>
import DictForm from './__components__/dictForm'
import { getHmdictionaryPageList,deleteHmdictionary } from '@/apis/paas/HmDictionaryController.js'

export default {
  name: 'Dict',
  components: { DictForm },
  data () {
    return {
      loading: false,
      queryData: { },
      pageData: {
        pageSize: 10,
        pageNo: 1
      },
      tableData: [],
      total: 0
    }
  },

  created () {
    this.loadData()
  },

  methods: {
    loadData (pageNo = this.pageData.pageNo) {
      this.loading = true
      getHmdictionaryPageList({
        ...this.queryData,
        ...this.pageData,
        pageNo
      }).then(res => {
        if (res.data.length === 0 && pageNo > 1) {
          this.loadData(pageNo - 1)
          return
        }
        this.tableData = res.data
        this.total = res.totalCount
        this.pageData = {
          pageSize: res.pageSize,
          pageNo: res.pageNo
        }
      }).finally(() => {
        this.loading = false
      })
    },

    handleReset () {
      this.queryData = {}
      this.loadData(1)
    },

    handleCreate () {
      this.$refs.DictForm.addForm()
    },

    handleEdit (record) {
      this.$refs.DictForm.editForm({
        ...record
      })
    },

    handleDelete (record) {
      deleteHmdictionary([record.id]).then(res => {
        this.loadData()
        this.$message.success('操作成功')
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
